<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";
import * as dayjs from "dayjs";

export default {
  name: "UpdateBirthday",
  props: ["value"],
  data() {
    return {
      minDate: new Date(1950, 1, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  methods: {
    // 点击确认
    async onConfirm() {
      const newDate = dayjs(this.currentDate).format("YYYY-MM-DD");
      if (newDate === this.value) return this.$emit("close");

      this.$toast.loading({
        message: "保存中...",
        forbidClick: true,
      });

      await updateUserProfile({
        birthday: newDate,
      });
      this.$emit("input", newDate);
      this.$emit("close");
      this.$toast.success("保存成功");
    },
  },
};
</script>

<style></style>
